package com.jiuzhe.commercial.base;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.view.CustomLoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends SuperBaseActivity {
    private BaseApplication mApplication;
    protected Context mContext;
    public View mView;
    private FrameLayout mFrameLayout; // 布局
    private TextView head_title; // 标题名字
    public TextView tv_right;
    private ImageButton ib_back;
    public CustomLoadingDialog dialog;
    private Unbinder bind;
    private static BaseActivity mForegroundActivity;
    private RelativeLayout mRl_head;


    /**
     * Android生命周期回调方法-创建
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApplication = (BaseApplication) getApplication();
        mContext = getApplicationContext();
        mApplication.addActvity(this);
        super.setContentView(R.layout.activity_main2);                  //主布局
        CustomLoadingDialog customProgressDialog = new CustomLoadingDialog(BaseActivity.this);

        dialog = CustomLoadingDialog.createDialog(BaseActivity.this);

        mFrameLayout = (FrameLayout) findViewById(R.id.fl_container);
        mRl_head = (RelativeLayout) findViewById(R.id.rl_head);
        head_title = (TextView) super.findViewById(R.id.head_title);
        ib_back = (ImageButton) super.findViewById(R.id.ib_back);
        tv_right = (TextView) super.findViewById(R.id.tv_right);
       // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initView();
    }


    protected void initView() {
        loadViewLayout();
        loadData();
    }


    @Override
    public void setContentView(int layoutResID) {
        mView = getLayoutInflater().inflate(layoutResID, null);
        setContentView(mView);
        bind = ButterKnife.bind(BaseActivity.this, mView);
    }

    public void setContentView(View view) {
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(view);
    }

    /**
     * 标题
     *
     * @param title
     */
    @Override
    public void setTitle(CharSequence title) {
        head_title.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        head_title.setText(titleId);
    }


    /**
     * 右边
     *
     * @param title
     */

    public void setRringTitle(CharSequence title) {
        tv_right.setText(title);
    }


    /**
     * 加载布局
     */
    protected abstract void loadViewLayout();


    /**
     * 向后台请求数据
     */
    protected abstract void loadData();


    // 返回
    public void back(View v) {
        onBackPressed();
    }


    /**
     * 返回键
     * @param visibility
     */
    public void setView(int visibility) {
        ib_back.setVisibility(visibility);
    }


    /**
     * 标题栏
     * @param visibility
     */
    public void setHead_title(int visibility) {
        mRl_head.setVisibility(visibility);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mContext = null;
        mFrameLayout = null;
        mView = null;
        head_title = null;
        dialog = null;
        mApplication.removeActvity(this);
        mApplication = null;
        mForegroundActivity = null;
        HttpClient.cancelRequest(this);
        if (bind != null) {
            bind.unbind();
        }
    }

    /**
     * 获取当前处于前台的activity
     */
    public static BaseActivity getForegroundActivity() {
        return mForegroundActivity;
    }
}
